﻿using Learun.Util;
using System;
using System.Collections.Generic;
using static Learun.Application.TwoDevelopment.ZZDT_EC.ProjectSugar;
using static Learun.Util.SqlSugar.SqlSugarHelper;

namespace Learun.Application.TwoDevelopment.ZZDT_EC
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创 建：超级管理员
    /// 日 期：2022-06-21 15:32
    /// 描 述：箱柜表
    /// </summary>
    public class ec_PanelBLL : ec_PanelIBLL
    {
        ec_PanelService ec_PanelService;
        public ec_PanelBLL()
        {
            ec_PanelService = new ec_PanelService();
        }
        #region 获取数据

        /// <summary>
        /// 获取列表数据
        /// <summary>
        /// <returns></returns>
        public IEnumerable<ec_PanelEntity> GetList(string queryJson, Pagination pagination = null)
        {
            try
            {
                var queryParam = queryJson.ToJObject();
                string ProjectId = queryParam["ProjectId"]?.ToString();
                string TagNumber = queryParam["TagNumber"]?.ToString();
                var tableName = TableName<ec_PanelEntity>(ProjectId);
                var enginedataTableName = TableName<ec_enginedataEntity>(ProjectId);
                var objecttypeTableName = TableName<ec_objecttypeEntity>(ProjectId);
                var query = Db.Queryable<ec_PanelEntity>().AS(tableName)
                    .LeftJoin<ec_enginedataEntity>((a, b) => a.EngineerDataID == b.EngineDataID).AS<ec_enginedataEntity>(enginedataTableName)
                    .LeftJoin<ec_objecttypeEntity>((a, b, c) => b.ObjectTypeID == c.ObjectTypeID).AS<ec_objecttypeEntity>(objecttypeTableName)
                    .WhereIF(!string.IsNullOrEmpty(TagNumber), (a, b) => b.TagNumber.Contains(TagNumber))
                    .Select((a, b, c) => new ec_PanelEntity()
                    {
                        PanelID = a.PanelID,
                        Panel_Loc_ID = a.Panel_Loc_ID,
                        CreateTime = a.CreateTime,
                        CreateUserID = a.CreateUserID,
                        UpdateTime = a.UpdateTime,
                        UpdateUserID = a.UpdateUserID,
                        EngineerDataID = a.EngineerDataID,
                        MaxStripNumber = a.MaxStripNumber,
                        TagNumber = b.TagNumber,
                        systempanel = c.SystemPanel.ToString(),
                    });
                List<ec_PanelEntity> list;
                if (pagination == null)
                {
                    list = query.ToList();
                }
                else
                {
                    int records = 0;
                    list = query.ToPageList(pagination.page, pagination.rows, ref records);
                    pagination.records = records;
                }
                return list;
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }
        /// <summary>
        /// 获取实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public ec_PanelEntity GetEntity(string ProjectId, string keyValue)
        {
            try
            {
                return ec_PanelService.GetEntity(ProjectId, keyValue);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }
 
        #endregion

        #region 提交数据

        /// <summary>
        /// 删除实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void DeleteEntity(string ProjectId, string keyValue)
        {
            try
            {
                ec_PanelService.DeleteEntity(ProjectId, keyValue);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        /// <summary>
        /// 保存实体数据（新增、修改）
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void SaveEntity(string ProjectId, string keyValue, ec_PanelEntity entity)
        {
            try
            {
                ec_PanelService.SaveEntity(ProjectId, keyValue, entity);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowBusinessException(ex);
                }
            }
        }

        #endregion

    }
}
